package edu.hubu.supermarketmanagementsystem.service.impl;

import edu.hubu.supermarketmanagementsystem.entity.Product;
import edu.hubu.supermarketmanagementsystem.mapper.ProductMapper;
import edu.hubu.supermarketmanagementsystem.service.ProductService;
import edu.hubu.supermarketmanagementsystem.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    // 添加商品
    @Override
    public ResultVO<String> addProduct(Product product) {
        try {
            if (product.getProductName() == null || product.getProductName().isEmpty()) {
                return new ResultVO<>(400, "Product name cannot be empty", null);
            }
            productMapper.insertProduct(product);
            return new ResultVO<>(200, "Product added successfully", null);
        } catch (Exception e) {
            return new ResultVO<>(500, "Error adding product: " + e.getMessage(), null);
        }
    }

    // 删除商品
    @Override
    public ResultVO<String> deleteProductById(int productId) {
        if (!isProductExists(productId)) {
            return new ResultVO<>(404, "Product not found", null);
        }
        productMapper.deleteProductById(productId);
        return new ResultVO<>(200, "Product deleted successfully", null);
    }

    // 更新商品
    @Override
    public ResultVO<String> updateProduct(Product product) {
        try {
            if (!isProductExists(product.getProductId())) {
                return new ResultVO<>(404, "Product not found", null);
            }
            productMapper.updateProduct(product);
            return new ResultVO<>(200, "Product updated successfully", null);
        } catch (Exception e) {
            return new ResultVO<>(500, "Error updating product: " + e.getMessage(), null);
        }
    }

    // 根据ID查询商品
    @Override
    public ResultVO<Product> getProductById(int productId) {
        Product product = productMapper.selectById(productId);
        if (product == null) {
            return new ResultVO<>(404, "Product not found", null);
        }
        return new ResultVO<>(200, "Success", product);
    }

    // 根据名称模糊查询
    @Override
    public ResultVO<List<Product>> searchProductsByName(String name) {
        List<Product> products = productMapper.searchByName(name);
        return new ResultVO<>(200, "Success", products);
    }

    // 分页查询所有商品
    @Override
    public ResultVO<List<Product>> getAllProducts(int page, int pageSize) {
        int offset = (page - 1) * pageSize;
        List<Product> products = productMapper.selectAll(offset, pageSize);
        return new ResultVO<>(200, "Success", products);
    }

    // 按类别筛选商品
    @Override
    public ResultVO<List<Product>> getProductsByCategory(String category) {
        List<Product> products = productMapper.selectByCategory(category);
        return new ResultVO<>(200, "Success", products);
    }

    // 更新库存
    @Override
    public ResultVO<String> updateStock(int productId, int stockDelta) {
        Product product = productMapper.selectById(productId);
        if (product == null) {
            return new ResultVO<>(404, "Product not found", null);
        }
        if (product.getStock() + stockDelta < 0) {
            return new ResultVO<>(400, "Insufficient stock", null);
        }
        productMapper.updateStock(productId, stockDelta);
        return new ResultVO<>(200, "Stock updated successfully", null);
    }

    // 检查商品是否存在
    @Override
    public boolean isProductExists(int productId) {
        return productMapper.existsById(productId) > 0;
    }
}
